n=int(input())
if n<999 or n>10000:
    print("0")
else:
    a=n//1000
    b=n//100-a*10
    c=n//10-n//100*10
    d=n%10
    k=0
    big=0
    for q in range(1,a+1):
        if (a%q==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    for w in range(1,b+1):
        if (b%w==0):
            k=k+1
    if k==2 and b>big:
        big=b
        k=k-k
    k=k-k
    for e in range(1,c+1):
        if (c%e==0):
            k=k+1
    if k==2 and c>big:
        big=c
        k=k-k
    k=k-k
    for r in range(1,d+1):
        if (d%r==0):
            k=k+1
    if k==2 and d>big:
        big=d
        k=k-k
    k=k-k
    a=n%100
    b=n//100
    c=(n//10)-(n//1000*100)

    for t in range(1,a+1):
        if (a%t==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    for y in range(1,b+1):
        if (b%y==0):
            k=k+1
    if k==2 and b>big:
        big=b
        k=k-k
    k=k-k
    for u in range(1,c+1):
        if (c%u==0):
            k=k+1
    if k==2 and c>big:
        big=c
        k=k-k
    k=k-k
    a=n//10
    b=n%1000

    for o in range(1,a+1):
        if (a%o==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    for x in range(1,b+1):
        if (b%x==0):
            k=k+1
    if k==2 and b>big:
        big=b
        k=k-k
    k=k-k
    a=n
    for l in range(1,a+1):
        if (a%l==0):
            k=k+1
    if k==2 and a>big:
        big=a
        k=k-k
    k=k-k
    print(big)
